#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Taotie Ren
# import logging 
# 禁用日志模块
# logging.disable(logging.CRITICAL)
# logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')

# resizeAndAddLogo。py - Resizes all image in current working directory to fit in a 300X300 square catlogo.png to the lower-right corner.
import os
from PIL import Image

SQUARE_FIT_SIZE = 300
os.makedirs('logo', exist_ok=True)
LOGO_FILENAME = "logo\\catlogo.png"
logoIm = Image.open(LOGO_FILENAME)
logoWidth, logoHeight = logoIm.size

os.makedirs('withlogo', exist_ok=True)
# Loop Over all files in the working directory.
for filename in os.listdir('LOGO'):
	if not (filename.endswith('.png') or filename.endswith('.jpg')) or filename == LOGO_FILENAME:
		continue        # Skip non-image files and the logo file itself

	im = Image.open(filename)
	width, height = im.size

	# Check if image needs to be resized.
	if width > SQUARE_FIT_SIZE and height > SQUARE_FIT_SIZE:
		# Calculate the new width and height to resize to.
		if width > height:
			height = int((SQUARE_FIT_SIZE / width) * height)
			width =SQUARE_FIT_SIZE
		else:
			width = int((SQUARE_FIT_SIZE / height) * width)
			height = SQUARE_FIT_SIZE
		# Resize the image.
		print('Resizing %s...' % (filename))
			im = im.resize((width, height))
		# Add the logo.
		print('Add logo to %s...' % (filename))
		im.paste(logoIm, (width - logoWidth, height - logoHeight), logoIm)

		#  Save changes.
		im.save(os.path.join('withlogo', filename))